@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$tree: () !default;
$tree: map.merge(
  (
    label-color: inherit,
    label-color-hover: get-css-var('tree-label-color'),
    label-color-focus: get-css-var('tree-label-color-hover'),
    label-color-selected: get-css-var('tree-label-color-hover'),
    label-color-secondary: get-css-var('content-color-secondary'),
    label-color-disabled: get-css-var('content-color-disabled'),
    label-color-readonly: get-css-var('tree-label-color'),
    label-bg-color: transparent,
    label-bg-color-hover: get-css-var('fill-color-hover'),
    label-bg-color-focus: get-css-var('tree-label-bg-color-hover'),
    label-bg-color-selected: get-css-var('color-primary-opacity-8'),
    label-bg-color-disabled: get-css-var('tree-label-bg-color'),
    label-bg-color-readonly: get-css-var('tree-label-bg-color'),
    label-radius: get-css-var('radius-base'),
    label-gap: 6px,
    node-span: 4px,
    arrow-size: 22px,
    arrow-color: inherit,
    arrow-color-disabled: get-css-var('content-color-disabled'),
    indicator-color: get-css-var('color-primary-base'),
    indent-width: 16px,
    depth: -1,
    // link-line-index: -1,
    link-line-type: 'dashed',
    link-line-color: get-css-var('border-color-base')
  ),
  $tree
);

.#{$namespace}-tree {
  &-vars {
    @include define-preset-values('tree', $tree);
  }

  @include basis;
  @include inherit-color;

  position: relative;

  $arrow-s: get-css-var('tree-arrow-size');
  $indent-w: get-css-var('tree-indent-width');
  $depth: get-css-var('tree-depth');

  $link-line: #{&}__link-line;

  &__link-line {
    position: absolute;
    pointer-events: none;
    border: 0 get-css-var('tree-link-line-type') get-css-var('tree-link-line-color');

    &--vertical {
      inset-inline-start: calc(#{$arrow-s} * 0.5 + #{$indent-w} * (#{$depth} - 1) - 1px);
      top: 0;
      bottom: 0;
      border-inline-start-width: 1px;
      transform: translateX(calc(-1 * #{get-css-var('tree-link-line-index')} * #{$indent-w}));

      @include rtl {
        transform: translateX(calc(#{get-css-var('tree-link-line-index')} * #{$indent-w}));
      }
    }

    &--horizontal {
      inset-inline-start: calc(#{$arrow-s} * 0.5 + #{$indent-w} * (#{$depth} - 1) - 1px);
      bottom: calc(50% - #{get-css-var('tree-node-span')});
      width: calc(#{$indent-w} * 0.5);
      border-bottom-width: 1px;
    }
  }

  &__list {
    padding: 0;
    margin: 0;
    list-style: none;
  }

  &__content {
    display: flex;
    align-items: center;
    padding-inline-start: calc(#{$depth} * #{$indent-w});

    &--effect {
      transition: get-css-var('transition-color'), get-css-var('transition-background');

      &:hover {
        color: get-css-var('tree-label-color-hover');
        background-color: get-css-var('tree-label-bg-color-hover');
      }
    }
  }

  &__label {
    display: inline-flex;
    flex-grow: 1;
    align-items: center;
    padding: 0 4px;
    color: get-css-var('tree-label-color');
    cursor: pointer;
    background-color: get-css-var('tree-label-bg-color');
    border-radius: get-css-var('tree-label-radius');

    &--effect {
      transition: get-css-var('transition-color'), get-css-var('transition-background');

      &:hover {
        color: get-css-var('tree-label-color-hover');
        background-color: get-css-var('tree-label-bg-color-hover');
      }
    }
  }

  $content: #{&}__content;
  $label: #{&}__label;

  &__node {
    padding-top: get-css-var('tree-node-span');
    list-style: none;
    outline: 0;

    &--link-line {
      position: relative;
    }

    &--last #{$link-line}--first {
      bottom: calc(50% - #{get-css-var('tree-node-span')});
    }

    &--no-arrow #{$link-line}--horizontal {
      width: calc(#{$indent-w} * 0.5 + #{$arrow-s});
    }

    @mixin effect {
      #{$content}--effect,
      #{$label}--effect {
        @content;
      }
    }

    &--focused {
      @include effect {
        color: get-css-var('tree-label-color-focus');
        background-color: get-css-var('tree-label-bg-color-focus');
      }
    }

    &--selected {
      @include effect {
        &,
        &:hover {
          color: get-css-var('tree-label-color-selected');
          background-color: get-css-var('tree-label-bg-color-selected');
        }
      }
    }

    &--secondary {
      @include effect {
        &,
        &:hover {
          color: get-css-var('tree-label-color-secondary');
        }
      }
    }

    // &--disabled,
    // &--disabled:hover {
    //   @include effect {
    //     color: get-css-var('tree-label-color-disabled');
    //     cursor: default;
    //     background-color: get-css-var('tree-label-bg-color-disabled');
    //   }
    // }

    &--readonly {
      @include effect {
        &,
        &:hover {
          color: get-css-var('tree-label-color-readonly');
          cursor: default;
          background-color: get-css-var('tree-label-bg-color-readonly');
        }
      }
    }

    &--is-floor {
      @include effect {
        &,
        &:hover {
          cursor: pointer;
        }
      }
    }
  }

  &__content--effect#{&}__content--disabled {
    &,
    &:hover {
      color: get-css-var('tree-label-color-disabled');
      cursor: default;
      background-color: get-css-var('tree-label-bg-color-disabled');
    }
  }

  &__label--effect#{&}__label--disabled {
    &,
    &:hover {
      color: get-css-var('tree-label-color-disabled');
      cursor: default;
      background-color: get-css-var('tree-label-bg-color-disabled');
    }
  }

  &__text {
    display: flex;
    flex-grow: 1;
    align-items: center;
    max-width: 100%;
  }

  &__prefix,
  &__suffix {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    min-width: 0;
  }

  &__prefix {
    margin-inline-end: get-css-var('tree-label-gap');
  }

  &__suffix {
    margin-inline-start: get-css-var('tree-label-gap');
  }

  &__arrow {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: get-css-var('tree-arrow-size');
    height: get-css-var('tree-arrow-size');
    margin-inline-end: 2px;
    color: get-css-var('tree-arrow-color');
    cursor: pointer;
    transition: get-css-var('transition-color');

    &--transparent {
      visibility: hidden;
      opacity: 0%;
    }

    &--expanded .#{$namespace}-icon {
      transform: rotate(90deg);

      @include rtl {
        transform: rotate(-90deg);
      }
    }

    &--disabled {
      color: get-css-var('tree-arrow-color-disabled');
      cursor: not-allowed;
    }
  }

  &__checkbox {
    display: inline-flex;
    margin-inline-end: 6px;

    &--suffix {
      margin-inline: 6px 0;
    }
  }

  &__indicator {
    position: absolute;
    inset-inline: 0 0;
    height: 1px;
    will-change: top, inset-inline-start;
    background-color: get-css-var('tree-indicator-color');
  }
}
